package com.ebelter.ty.ui.activity;

import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import com.ebelter.btcomlib.bases.activity.BaseActivity;
import com.ebelter.btcomlib.models.https.NetUtils;
import com.ebelter.btcomlib.models.https.interfaces.HttpResponse;
import com.ebelter.btcomlib.models.https.responses.BaseResponse;
import com.ebelter.btcomlib.utils.MD5Utils;
import com.ebelter.btcomlib.utils.SpUtil;
import com.ebelter.btcomlib.utils.StringUtils;
import com.ebelter.btcomlib.utils.ToastUtil;
import com.ebelter.btcomlib.utils.VerifyUtils;
import com.ebelter.btcomlib.utils.log.LogUtils;
import com.ebelter.ty.R;
import com.ebelter.ty.adapters.FindPswPagerAdapter;
import com.ebelter.ty.model.http.TyNetUtil;
import com.ebelter.ty.ui.fragment.scale.findpsw.BasePagerFindPsw;
import com.ebelter.ty.ui.fragment.scale.findpsw.FindPswPage_Email;
import com.ebelter.ty.ui.fragment.scale.findpsw.FindPswPage_Psw;
import java.util.ArrayList;
import java.util.List;
import static com.ebelter.btcomlib.views.CountDownTextView.LAST_SEND_CODE_TIME;


//找回密码界面
public class FindPswActivity extends BaseActivity implements View.OnClickListener {

    private static final String TAG = "FindPswActivity";


    ImageView iv_back_register;


    ViewPager vpRegister;


    TextView btn_next_register;

    private List<BasePagerFindPsw> pagers;


    @Override
    protected int loadLayoutById() {
        return R.layout.activity_register;
    }

    @Override
    protected void initView() {
        super.initView();
        iv_back_register=findViewById(R.id.iv_back_register);
        vpRegister=findViewById(R.id.vp_register);
        btn_next_register=findViewById(R.id.btn_next_register);
        iv_back_register.setOnClickListener(this);
        btn_next_register.setOnClickListener(this);
    }

    @Override
    protected void initData() {
        SpUtil.writeLong(LAST_SEND_CODE_TIME,0);
        if (pagers == null) {
            pagers = new ArrayList<>();
        } else {
            pagers.clear();
        }
        pagers.add(new FindPswPage_Email(this));
        pagers.add(new FindPswPage_Psw(this));


        vpRegister.setAdapter(new FindPswPagerAdapter(pagers));
        vpRegister.setOffscreenPageLimit(pagers.size());
        btn_next_register.setEnabled(false);


    }





    private int curIndex = 0;
    private void iv_back() {
        --curIndex;
        if (curIndex<0){
            finish();
        }else {
            vpRegister.setCurrentItem(curIndex);
            btn_next_register.setText(R.string.next);
        }

    }

    private void next() {
        if (curIndex==0){
            FindPswPage_Email mFindPswPage_Email = (FindPswPage_Email) pagers.get(0);
            String email=mFindPswPage_Email.getEmail();
            if (TextUtils.isEmpty(email)){
                ToastUtil.show(R.string.Email_must_not_be_empty);
                return;
            }
            if (!VerifyUtils.emailFormat(email)){
                ToastUtil.show(R.string.Incorrect_email_format);
                mFindPswPage_Email.shake_email_et();
                return;
            }
            String emailCode=mFindPswPage_Email.getEmailCode();
            if (TextUtils.isEmpty(emailCode)){
                ToastUtil.show(R.string.Mail_verification_code_cannot_be_empty);
                return;
            }
        }
        curIndex++;
        if (curIndex>=pagers.size()){
            curIndex=pagers.size()-1;
        }
        if (curIndex==pagers.size()-1&& TextUtils.equals(StringUtils.getResStr(R.string.done),btn_next_register.getText().toString().trim())){
            done();
            return;
        }
        if (curIndex==pagers.size()-1){
            btn_next_register.setText(R.string.done);
        }
        vpRegister.setCurrentItem(curIndex);

    }

    public void setNextButton(boolean enable){
        if (btn_next_register!=null) btn_next_register.setEnabled(enable);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode == KeyEvent.KEYCODE_BACK) { //屏蔽返回键
            onClick(iv_back_register);
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();

    }

    private void done(){
        LogUtils.i(TAG,"------done-----");
        FindPswPage_Email mFindPswPage_Email = (FindPswPage_Email) pagers.get(0);


        String email=mFindPswPage_Email.getEmail();

        if (TextUtils.isEmpty(email)){
            ToastUtil.show(R.string.Email_must_not_be_empty);
            return;
        }
        if (!VerifyUtils.emailFormat(email)){
            ToastUtil.show(R.string.Incorrect_email_format);
            mFindPswPage_Email.shake_email_et();
            return;
        }
        String emailCode=mFindPswPage_Email.getEmailCode();
        if (TextUtils.isEmpty(emailCode)){
            ToastUtil.show(R.string.Mail_verification_code_cannot_be_empty);
            return;
        }



        FindPswPage_Psw mFindPswPage_Psw = (FindPswPage_Psw) pagers.get(1);

        String psw1=mFindPswPage_Psw.getPsw1();
        String psw2=mFindPswPage_Psw.getPsw2();

        if (TextUtils.isEmpty(psw1)){
            ToastUtil.show(R.string.The_password_cannot_be_empty);
            return;
        }
        if (!TextUtils.equals(psw1,psw2)){
            ToastUtil.show(R.string.The_two_passwords_are_inconsistent);
            return;
        }

        if (psw1.length()<6){
            ToastUtil.show(R.string.psw_less_tip);
            return;
        }
        if (psw1.length()>20){
            ToastUtil.show(R.string.psw_over_tip);
            return;
        }

        if (!NetUtils.available()){
            ToastUtil.show(R.string.Network_error);
            return;
        }

        TyNetUtil.getInstance().findPassWord(this, email, MD5Utils.getMd5(psw1), emailCode, new HttpResponse<BaseResponse>() {
            @Override
            public void result(boolean isRequestSuccess, String requestDes, BaseResponse baseResponse,String s) {
                if (isRequestSuccess){
                    if (baseResponse.resultCode==0){
                        ToastUtil.show(R.string.Modify_Success);
                        finish();
                    }else if (baseResponse.resultCode==117){
                        ToastUtil.show(R.string.Email_is_not_registered);
                    }else if (baseResponse.resultCode==106){
                        ToastUtil.show(R.string.Email_verification_code_has_expired);

                    }else if (baseResponse.resultCode==107){
                        ToastUtil.show(R.string.Wrong_email_verification_code);
                    }else if(baseResponse.resultCode==110){
                        ToastUtil.show(R.string.An_exception_occurred_on_the_server);
                    }else {
                        ToastUtil.show(R.string.Modify_Failed);
                    }
                }else {
                    ToastUtil.show(R.string.Network_connection_error);
                }
            }
        });









    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.iv_back_register://返回键
                iv_back();
                break;
            case R.id.btn_next_register://下一步
                next();
                break;
        }
    }
}
